import AUTH from "../../../utils/auth";
import { getDistributionCenter } from "../../../api/cloudPointController";
import { getTradeOrderListByCustIdAndStatus, acceptOrder, completeOrder, orderTransfer } from "../../../api/tradeOrderController";
import { getFreightCenter } from "../../../api/custCarriageController";
import { successfulWithdrawalTmplIds } from "../../../utils/config";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    navList:[
      {
        name:'待处理配送',
        url:'/static/images/dclps.png',
      },
      {
        name:'已完成配送',
        url:'/static/images/ywcps.png',
      },
    ],
    navListIndex: 0,

    userBase:'',

    integralDetails:'',   //积分详情

    freightDetail: '', //运费详情

    readyDistribution: [],    //待配送

    status: '',    //查询列表 待处理30 和 已完成40

    content:{
      title:'转让',
      message:'',
      modalView: false,
    },

    realNameContent:{
      modalView: false,
      title: '温馨提示',
      message: '需要完成实名认证，才可以提现！'
    },
    tradeOrderId:'',          //订单id
    oldDistributionCenterId: '',  //当前配送人id

  },
  onShow(){
    this.setData({
      userBase: wx.getStorageSync('userBase')
    })

    this.getDistributionCenter()

    this.getTradeOrderListByCustIdAndStatus()

    this.getFreightCenter()
  },
  /**
   * 运费中心
   */
  async getFreightCenter(){
    let resolve = await getFreightCenter({
      custId: wx.getStorageSync('custId')
    })
    if (resolve.success) {
      this.setData({
        freightDetail: resolve.data
      })
    }
  },
  /**
   * 待处理配送   已完成配送
   */
  async getTradeOrderListByCustIdAndStatus(){
    let resolve = await getTradeOrderListByCustIdAndStatus({
      custId: wx.getStorageSync('custId'),
	    status: this.data.status
    })
    this.setData({
      readyDistribution: resolve.data
    })
  },
  /**
   * 配送中心
   */
  async getDistributionCenter(){
    let resolve = await getDistributionCenter({
      custId: wx.getStorageSync('custId'),
    })
    this.setData({
      integralDetails: resolve.data,
    })
  },
  /**
   * 运费提现
   */
  extract(){
    // 是否实名
    let realName =  AUTH.checkRealName()
    if (!realName) {
      this.setData({
        ['realNameContent.modalView']: true
      })
      return
    }else{
      // if (this.data.freightDetail==null) {
      //   wx.navigateTo({
      //     url: '/pages/freightCenter/cashing/index?id='+'',
      //   })
      // }else{
        wx.requestSubscribeMessage({
          tmplIds: successfulWithdrawalTmplIds,
          complete: ()=>{
            wx.navigateTo({
              url: '/pages/freightCenter/cashing/index',
            })
          }
        })
  
      // }
    }
  },
  /**
   * 切换导航
   */
  changeNav(e){
    let { index } = e.currentTarget.dataset
    this.setData({
      navListIndex: index
    })
    if (this.data.navListIndex==0) {
      this.setData({
        status: ''
      })
      this.getTradeOrderListByCustIdAndStatus()
    }else{
      this.setData({
        status: 40
      })
      this.getTradeOrderListByCustIdAndStatus()
    }
  },
  /**
   * 接单 0     转让  1       完成  2
   */
  async handlerClick(e){
    // 是否实名
    let realName =  AUTH.checkRealName()
    if (!realName) {
      this.setData({
        ['realNameContent.modalView']: true
      })
      return
    }

    let { index, id, distributioncenterid } = e.currentTarget.dataset
    if (index == 0) {
      let resolve = await acceptOrder({
        tradeOrderId: id
      })
      if (resolve.success) {
        this.getTradeOrderListByCustIdAndStatus()
        this.getDistributionCenter()
      }else{
        wx.showToast({
          title: resolve.errorMessage,
          icon: 'error'
        })
      }
    } else if (index == 1) {
      this.setData({
        ['content.modalView']: true,
        tradeOrderId: id,
        oldDistributionCenterId: distributioncenterid
      })
    } else if (index == 2) {
      let resolve = await completeOrder({
        tradeOrderId: id
      })
      if (resolve.success) {
        this.getTradeOrderListByCustIdAndStatus()
        this.getDistributionCenter()
      }else{
        wx.showToast({
          title: resolve.errorMessage,
          icon: 'error'
        })
      }
    }
  },
  /**
   * 弹窗 输入框内容
   */
  handlerInput(e){
    //配送人id
    let distributionCenterId = e.detail
    this.setData({
      ['content.message']: distributionCenterId
    })
  },
    /**
   * 弹窗确认
   */
  async handlerOk(){
    let self = this
    if (self.data.content.message== '') {
      wx.showToast({
        title: '请输入转让人ID',
        icon: 'error'
      })
      return
    }


    let resolve = await orderTransfer({
      id: self.data.tradeOrderId,
      distributionCenterId: self.data.content.message,
      oldDistributionCenterId:  self.data.oldDistributionCenterId
    })
    if (resolve.success) {
      wx.showToast({
        title: '转让成功！',
        success: ()=>{
          setTimeout(() => {
            self.getTradeOrderListByCustIdAndStatus()
            self.setData({
              ['content.modalView']: false,
              ['content.message']: '',
            })
          }, 1500);
        }
      })
    }else{
      wx.showToast({
        icon:'error',
        title: resolve.errorMessage,
      })
    }
    
    
  },
    /**
   * 弹窗取消
   */
  handlerNo(){
    this.setData({
      ['content.modalView']: false,
      ['content.message']: '',
    })
  },
  /**
   * 认证弹窗确认
   */
  realNameHandlerOk(){
    wx.navigateTo({
      url: '/pages/registerRealName/index',
    })
    this.setData({
      ['realNameContent.modalView']: false,
    })
  },
    /**
   * 认证弹窗取消
   */
  realNameHandlerNo(){
    this.setData({
      ['realNameContent.modalView']: false,
    })
  },
  /**
   * 拨号
   */
  makePhoneCall(e) {
    let {
      mobile
    } = e.currentTarget.dataset
    wx.makePhoneCall({
      phoneNumber: mobile //仅为示例，并非真实的电话号码
    })
  },
})